﻿using Models;
using Newtonsoft.Json;
using OSSUtility.BLL;
using OSSUtility.Models;
using Ozone.DAL;
using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class MethodBLL
    {

        private static List<execution_method> _allexecution_method;

        /// <summary>
        /// 查询方法列表
        /// </summary>
        /// <returns></returns>
        public static List<Method> GetALLMethodList()
        {
            List<Method> methodList = new List<Method>();
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                //根据ID查询
                _allexecution_method = db.Queryable<execution_method>()
                .OrderBy(p => p.CreateTime, OrderByType.Desc)
                .ToList();

                if (_allexecution_method != null && _allexecution_method.Count > 0)
                {
                    methodList = ConvertToMethodList(_allexecution_method);

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询方法列表发生异常", ex);
                _allexecution_method = null;

            }
            return methodList;
        }

        /// <summary>
        /// 添加方法信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddMethod(string methodName, out string Message)
        {
            bool IsSuccess = false;
            Message = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(methodName))
                {
                    List<execution_method> list = MethodDAL.GetALLMethod(methodName);
                    if (list.Count == 0)
                    {
                        execution_method execution_Method = new execution_method();
                        execution_Method.FID = Guid.NewGuid().ToString();
                        execution_Method.MethodName = methodName;
                        execution_Method.ExecSerialNumber = 0;
                        execution_Method.ExecSerialStepNumber = 0;
                        execution_Method.MethodState = 0;
                        execution_Method.CreateTime = DateTime.Now;
                        execution_Method.ModifyTime = DateTime.Now;
                        execution_Method.MethodConten = "";
                        IsSuccess = MethodDAL.AddMethod(execution_Method);
                    }
                    else
                    {
                        Message = "方法名称:" + methodName + "已存在，请重新输入!";
                        IsSuccess = false;
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加方法异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }




        /// <summary>
        /// 删除方法信息
        /// </summary>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static bool DeleteMethod(string FID)
        {
            bool IsSuccess = false;
            try
            {
                if (!string.IsNullOrEmpty(FID))
                {
                    IsSuccess = MethodDAL.DeleteMethod(FID);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除方法异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool UpdateMethod(Method Model, out string Message)
        {
            bool IsSuccess = false;
            Message = string.Empty;
            try
            {
                List<execution_method> list = MethodDAL.GetALLMethod(Model.MethodName);
                if (list.Count == 0)
                {
                    execution_method execution_Method = new execution_method();
                    execution_Method.FID = Model.FID;
                    execution_Method.MethodName = Model.MethodName;
                    IsSuccess = MethodDAL.UpdateMethod(execution_Method);
                }
                else
                {
                    Message = "方法名称：" + Model.MethodName + "已存在,请重新输入";
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error("修改日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }


        /// <summary>
        /// 修改方法序列
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="SerialNumber"></param>
        /// <returns></returns>
        public static bool UpdateMethodExecSerial(string FID, List<MethodItem> methodItem)
        {
            bool IsSuccess = false;
            try
            {
                string MethodConten = JsonConvert.SerializeObject(methodItem);
                IsSuccess = MethodDAL.UpdateMethodExecSerial(FID, MethodConten);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新方法序列异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改方法执行序列ID
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="ExecSerialNumber"></param>
        /// <returns></returns>
        public static bool UpdateMethodExecSerialNumber(string FID, string ExecSerialNumber)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID) && !string.IsNullOrEmpty(ExecSerialNumber))
                {
                    IsSuccess = db.Updateable<execution_method>()
                    .SetColumns(p => p.ExecSerialNumber == Convert.ToInt32(ExecSerialNumber))
                    .Where(p => p.FID == FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改执行方法序列异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }



        /// <summary>
        /// 修改方法执行序列步骤编号
        /// </summary>
        /// <param name="FID"></param>
        /// <param name="ExecSerialStepNumber"></param>
        /// <returns></returns>
        public static bool UpdateMethodExecSerialStepNumber(string FID, string ExecSerialStepNumber)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(FID) && !string.IsNullOrEmpty(ExecSerialStepNumber))
                {
                    IsSuccess = db.Updateable<execution_method>()
                    .SetColumns(p => p.ExecSerialStepNumber == Convert.ToInt32(ExecSerialStepNumber))
                    .Where(p => p.FID == FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("修改执行方法序列步骤异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }




        public static List<Method> ConvertToMethodList(List<execution_method> sourceList)
        {
            if (sourceList == null) return new List<Method>();

            var result = new List<Method>();
            foreach (var item in sourceList)
            {
                var method = new Method
                {
                    FID = item.FID,
                    MethodName = item.MethodName,
                    ExecSerialNumber = item.ExecSerialNumber,
                    ExecSerialStepNumber = item.ExecSerialStepNumber,
                    MethodState = item.MethodState,
                    MethodConten = item.MethodConten,
                    CreateTime = item.CreateTime,
                    ModifyTime = item.ModifyTime
                };

                // 反序列化JSON到methodItem列表
                if (!string.IsNullOrEmpty(item.MethodConten))
                {
                    try
                    {
                        method.methodItem = JsonConvert.DeserializeObject<List<MethodItem>>(item.MethodConten);
                    }
                    catch
                    {
                        method.methodItem = new List<MethodItem>();
                    }
                }
                else
                {
                    method.methodItem = new List<MethodItem>();
                }

                result.Add(method);
            }
            return result;
        }
    }
}
